package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.system.convert.CarsConverter;
import com.ruoyi.system.dl.DeleteImages;
import com.ruoyi.system.domain.Images;
import com.ruoyi.system.domain.dto.CarsDto;
import com.ruoyi.system.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarsMapper;
import com.ruoyi.system.domain.Cars;
import com.ruoyi.system.service.ICarsService;

/**
 * 汽车管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class CarsServiceImpl implements ICarsService {
    @Autowired
    private CarsMapper carsMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    /**
     * 查询汽车管理
     *
     * @param id 汽车管理主键
     * @return 汽车管理
     */
    @Override
    public Cars selectCarsById(Long id) {
        Cars cars = carsMapper.selectCarsById(id);
        cars.setCarImgs(imagesMapper.selectImages("car" + cars.getId()));
        System.out.println(cars);
        return cars;
    }

    /**
     * 查询汽车管理列表
     *
     * @param cars 汽车管理
     * @return 汽车管理
     */
    @Override
    public List<Cars> selectCarsList(Cars cars) {
        List<Cars> carss = carsMapper.selectCarsList(cars);

        return carss.stream().peek(o -> {
            List<String> strings = imagesMapper.selectImages("car" + o.getId());
            o.setCarImgs(strings);
        }).collect(Collectors.toList());
    }

    /**
     * 新增汽车管理
     *
     * @param
     * @return 结果
     */
    @Override
    public int insertCars(CarsDto carsDto) {
        Cars cars = CarsConverter.INSTANCE.dtoToEntity(carsDto);
        int insertCars = carsMapper.insertCars(cars);
        //新增汽车成功后通过信息查询出这个汽车的id
        Cars carsid = carsMapper.selectCars(cars.getName(),cars.getCarBrand(),cars.getCarType());
        //拿到汽车id
        Long carsId = carsid.getId();
        List<String> carImgs = carsDto.getCarImgs();
        for(String str: carImgs){
            Images images = new Images(null,"car"+carsId,str);
            imagesMapper.insert(images);
        }
        return insertCars;
    }

    /**
     * 修改汽车管理
     *
     * @param cars 汽车管理
     * @return 结果
     */
    @Override
    public int updateCars(Cars cars) {
        int i = carsMapper.updateCars(cars);

        String foreignId = "car" + cars.getId();

        DeleteImages.deleteIma(foreignId,imagesMapper);
        // 将所有与该id的图片删除
        int i1 = imagesMapper.deleteByForeginId(foreignId);
        System.out.println(i1);

        // 转换set数组
        Set<String> carImages = new HashSet<>(cars.getCarImgs());

        // 从新添加
        carImages.forEach(o -> imagesMapper.insert(new Images(null,foreignId,o)));
        return i;
    }

    /**
     * 批量删除汽车管理
     *
     * @param ids 需要删除的汽车管理主键
     * @return 结果
     */
    @Override
    public int deleteCarsByIds(Long[] ids) {
        Arrays.stream(ids).forEach(o -> {
            imagesMapper.deleteByForeginId("car"+o);
            DeleteImages.deleteIma("car"+o,imagesMapper);
        });

        return carsMapper.deleteCarsByIds(ids);
    }

    /**
     * 删除汽车管理信息
     *
     * @param id 汽车管理主键
     * @return 结果
     */
    @Override
    public int deleteCarsById(Long id) {
        String foreignId = "car"+id;
        DeleteImages.deleteIma(foreignId,imagesMapper);
        imagesMapper.deleteByForeginId(foreignId);
        return carsMapper.deleteCarsById(id);
    }
}
